package com.jastar.android.game.richman.logic;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

import androidx.appcompat.content.res.AppCompatResources;

import com.jastar.android.game.richman.logic.bean.Cell;
import com.jastar.android.game.richman.logic.bean.CellGroup;
import com.jastar.android.game.richman.logic.bean.Player;
import com.jastar.android.game.richman.logic.enums.CellType;
import com.jastar.android.game.richman.logic.enums.LandType;
import com.jastar.android.test.R;
import com.jastar.android.util.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Game {

    public static final boolean FAST_MODE = false;

    public static final int[] dicePics = {R.drawable.pic_dice_1, R.drawable.pic_dice_2, R.drawable.pic_dice_3, R.drawable.pic_dice_4, R.drawable.pic_dice_5, R.drawable.pic_dice_6};
    public static final int LINE_COLOR = 0xFF333333;
    public static final Player[] ALL_PLAYERS;

    static {
        ALL_PLAYERS = new Player[4];
        ALL_PLAYERS[0] = new Player("哪吒", 10, 0xFFFF6666, R.drawable.pic_character_nezha);
        ALL_PLAYERS[1] = new Player("敖丙", 10, 0xFF66DDDD, R.drawable.pic_character_aobing);
        ALL_PLAYERS[2] = new Player("奥特曼", 10, 0xFFFFCC66, R.drawable.pic_character_ultraman);
        ALL_PLAYERS[3] = new Player("蜘蛛侠", 10, 0xFF6666FF, R.drawable.pic_character_spiderman);
    }

    protected Map<String, Drawable> levelHouseIcons;
    protected Paint paint = new Paint();
    protected Resources resources;
    protected Bitmap[] diceBitmaps;
    protected int lineAverage;

    protected List<Cell> cells;
    protected List<Player> players;
    protected int currentPlayer = 0;

    protected volatile int diceResult;

    /** 随机变卖，直到余额大于等于0 */
    public boolean randomSale(Player player) {
        Random random = new Random();
        List<Cell> ownCells = cells.stream().filter(c -> player.equals(c.getOwner())).collect(Collectors.toList());
        while (!ownCells.isEmpty() && player.getMoney() < 0) {
            Cell cell = ownCells.get(random.nextInt(ownCells.size()));

            player.receive(cell.getLevel() * cell.getPrice());
            cell.clean();

            ownCells.remove(cell);
        }
        return player.getMoney() >= 0;
    }

    public void moveCurrentPlayer(int step) {
        Player player = getCurrentPlayer();
        Utils.sleep(FAST_MODE ? 1 : 300);
        IntStream.rangeClosed(1, step).forEach(i -> {
            int newPosition = (player.getPosition() + 1) % cells.size();
            player.setPosition(newPosition);
            Utils.sleep(FAST_MODE ? 1 : 300);
        });
    }

    public void removeCurrentPlayer() {
        players.remove(currentPlayer);
        currentPlayer = currentPlayer % players.size();
    }

    public void changeNextPlayer() {
        currentPlayer = (currentPlayer + 1) % players.size();
    }

    public Player getCurrentPlayer() {
        return players.get(currentPlayer);
    }

    public Game(Resources resources, int playerCount, int money) {
        this.resources = resources;

        initCells();
        initPlayers(playerCount, money);
        initDiceBitmaps();
    }

    public void initLevelHouseIcons(Context context) {
        levelHouseIcons = new HashMap<>();
        levelHouseIcons.put(LandType.CHAIN.name() + 1, AppCompatResources.getDrawable(context, R.drawable.pic_chain_level_1));
        levelHouseIcons.put(LandType.CHAIN.name() + 2, AppCompatResources.getDrawable(context, R.drawable.pic_chain_level_2));
        levelHouseIcons.put(LandType.CHAIN.name() + 3, AppCompatResources.getDrawable(context, R.drawable.pic_chain_level_3));
        levelHouseIcons.put(LandType.HOUSE.name() + 1, AppCompatResources.getDrawable(context, R.drawable.pic_house_level_1));
        levelHouseIcons.put(LandType.HOUSE.name() + 2, AppCompatResources.getDrawable(context, R.drawable.pic_house_level_2));
        levelHouseIcons.put(LandType.HOUSE.name() + 3, AppCompatResources.getDrawable(context, R.drawable.pic_house_level_3));
        levelHouseIcons.put(LandType.HOUSE.name() + 4, AppCompatResources.getDrawable(context, R.drawable.pic_house_level_4));
        levelHouseIcons.put(LandType.HOUSE.name() + 5, AppCompatResources.getDrawable(context, R.drawable.pic_house_level_5));
    }

    private void initCells() {
        int index = 0;
        cells = new ArrayList<>();
        CellGroup group1 = new CellGroup(1, 0xFFB4D8C8);
        CellGroup group2 = new CellGroup(2, 0xFFFFC8D8);
        CellGroup group3 = new CellGroup(3, 0xFFFFE4B4);
        CellGroup group4 = new CellGroup(4, 0xFFB4E8FF);
        CellGroup group5 = new CellGroup(5, 0xFFE8C8FF);
        CellGroup group6 = new CellGroup(6, 0xFFC8E8B4);
        CellGroup group7 = new CellGroup(7, 0xFFFFD8B4);
        CellGroup group8 = new CellGroup(8, 0xFFD8D8C8);
        cells.add(new Cell(null, CellType.START, index++, 0));
        cells.add(new Cell(group1, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group1, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group1, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group2, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group2, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(null, CellType.CHANCE, index++, 0));
        cells.add(new Cell(group3, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group3, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group4, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group4, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group4, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(null, CellType.DISASTER, index++, 0));
        cells.add(new Cell(group5, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group5, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group5, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group6, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group6, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(null, CellType.SHOP, index++, 0));
        cells.add(new Cell(group7, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group7, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group8, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group8, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        cells.add(new Cell(group8, CellType.LAND, index++, Cell.DEFAULT_PRICE));
        lineAverage = cells.size() / 4;
        Cell.initInstances(cells);
    }

    private void initPlayers(int playerCount, int money) {
        players = new ArrayList<>();
        IntStream.range(0, playerCount).forEach(i -> {
            Player p = ALL_PLAYERS[i];
            players.add(new Player(p.getName(), money, p.getColor(), p.getIcon()));
        });
    }

    private void initDiceBitmaps() {
        diceBitmaps = new Bitmap[dicePics.length];
        IntStream.range(0, dicePics.length).forEach(i -> {
            Bitmap bitmap = BitmapFactory.decodeResource(resources, dicePics[i]);
            Bitmap thumbnailBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
            diceBitmaps[i] = thumbnailBitmap;
        });
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public List<Cell> getCells() {
        return cells;
    }

    public void setCells(List<Cell> cells) {
        this.cells = cells;
    }

    public int getLineAverage() {
        return lineAverage;
    }

}
